<?php
namespace app\mec\controller;
use think\Db;
class Tixian extends Base {
// 定义配置项
    private $config=array(
        'APPID'              => 'wx79afdec9ad2e67a2', // 微信支付APPID.
        'MCHID'              => '1557327661', // 微信支付MCHID 商户收款账号
        'KEY'                => 'qAIpKcjJQrgQJMbzfNqYoBBgFj1STO31', // 微信支付KEY
        'APPSECRET'          => '81b17ac2628875727b2393c1bb285e05', // 公众帐号secert (公众号支付专用)
        'NOTIFY_URL'         => '', // 接收支付状态的连接

    );
    //----------------------------提现 S-----------------------------------
    /**
     * 获取提现数据
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            $map['ask_fen_id'] = session('mid');
            if(isset($key)&&$key!="")
            {
                $map['ask_number'] = ['like',"%" . $key . "%"];
            }
            if(isset($level)&&$level!="")
            {
                $map['a.ask_status'] = $level;
            }

            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_ask_money')->alias('a')
                ->join('up_fenxiao f','a.ask_fen_id=f.fen_id','LEFT')
                ->where($map)
                ->count();
            $lists = Db::table('up_ask_money')->alias('a')
                ->join('up_fenxiao f','a.ask_fen_id=f.fen_id','LEFT')
                ->where($map)
                ->order('ask_status asc')
                ->page($Nowpage, $limits)
                ->field('a.*,fen_id,fen_realname,fen_phone')
                ->select();//计算总页面
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch("tixian/index");
    }

    /**
     * 提交审核
     * @return mixed|\think\response\Json
     */
    public function askask(){
        if(request()->isPost()){
            $money = input('ask_money');
            $openid = input('ask_openid');
            if(empty($money) || empty($openid)){
                return json(['code' => 100, 'msg' => '有未填项！']);
            }
            $money = sprintf("%.2f",$money);
            if($money < 0.3 || $money > 5000){
                return json(['code' => 100, 'msg' => '单次提现金额不能低于0.3元或高于5000元']);
            }
            $mymoney = Db::name('fenxiao')->where('fen_id',session('mid'))->value('fen_money');
            if($money > $mymoney){
                return json(['code' => 100, 'msg' => '余额不足']);
            }
            $data = [
                'ask_number' => time() . rand(10000000,99999999),
                'ask_fen_id' => session('mid'),
                'ask_money' => $money,
                'ask_openid' => $openid,
                'ask_time' => date('Y-m-d H:i:s')
            ];
            Db::startTrans();
            try{
                Db::table('up_ask_money')->insert($data);
                Db::table('up_fenxiao')->where('fen_id', session('mid'))
                    ->setDec('fen_money',$money);
                Db::commit();
                return json(['code' => 200, 'msg' => '提交成功']);
            }catch (\Exception $e) {
                Db::rollback();
                return json(['code' => 100, 'msg' => '提交失败，请刷新重试！']);
            }
        }
        return $this->fetch('tixian/ask');
    }

}